package xyz.yohao.flow.admin.controller.flow;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.yohao.flow.bean.dto.flow.WorkflowCategoryDto;
import xyz.yohao.flow.bean.entity.WorkflowCategory;
import xyz.yohao.flow.common.constants.EntityConstants;
import xyz.yohao.flow.common.core.domain.AjaxResult;
import xyz.yohao.flow.common.exception.YamiShopBindException;
import xyz.yohao.flow.common.util.PageParam;
import xyz.yohao.flow.service.v2.WorkflowCategoryService;

import javax.validation.Valid;

@RestController
@RequestMapping("wf/category")
public class WorkflowCategoryController extends BaseFlowController {


    private final WorkflowCategoryService workflowCategoryService;

    public WorkflowCategoryController(WorkflowCategoryService workflowCategoryService) {
        this.workflowCategoryService = workflowCategoryService;
    }

    /**
     * 分页获取
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('admin:message:page')")
    public ResponseEntity<IPage<WorkflowCategory>> page(WorkflowCategoryDto dto, PageParam<WorkflowCategory> page) {

        IPage<WorkflowCategory> list = workflowCategoryService.page(page, new LambdaQueryWrapper<WorkflowCategory>()
                .eq(WorkflowCategory::getIsDeleted, EntityConstants.ENTITY_NOT_DEL_FLAT)
                .like(StrUtil.isNotBlank(dto.getCategoryName()), WorkflowCategory::getCategoryName, dto.getCategoryName()));

        return ResponseEntity.ok(list);
    }


    /**
     * 保存流程类别
     *
     * @param dto 保存实体
     * @return AjaxResult
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('wf:category:save')")
    public AjaxResult save(@Valid @RequestBody WorkflowCategoryDto dto) {

        WorkflowCategory category = mapperFacade.map(dto, WorkflowCategory.class);

        setBaseEntity(category);

        boolean isAny = workflowCategoryService.count(new LambdaQueryWrapper<WorkflowCategory>()
                .eq(WorkflowCategory::getCategoryName, category.getCategoryName())
                .eq(WorkflowCategory::getTenantId, category.getTenantId())) > 0;
        if (isAny) {
            throw new YamiShopBindException("该分类已经存在！");
        }

        workflowCategoryService.save(category);

        return AjaxResult.success();
    }

    /**
     * 更新流程类别
     *
     * @param dto 保存实体
     * @return AjaxResult
     */
    @PutMapping
    //   @PreAuthorize("@pms.hasPermission('wf:category:update')")
    public AjaxResult update(@Valid @RequestBody WorkflowCategoryDto dto) {

        WorkflowCategory category = mapperFacade.map(dto, WorkflowCategory.class);

        setBaseEntity(category);

        boolean exist = workflowCategoryService.count(new LambdaQueryWrapper<WorkflowCategory>()
                .eq(WorkflowCategory::getCategoryName, category.getCategoryName())
                .eq(WorkflowCategory::getTenantId, category.getTenantId())
                .ne(WorkflowCategory::getId, category.id)) > 0;

        if (exist) {
            throw new YamiShopBindException("该分类已经存在！");
        }
        WorkflowCategory model = workflowCategoryService.getById(category.getId());

        model.setCategoryName(category.getCategoryName());
        model.setRemark(category.getRemark());
        model.setParentId(category.getParentId());

        boolean isSuccess = workflowCategoryService.updateById(model);

        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * 删除流程类别
     *
     * @param categoryId 类别id
     * @return AjaxResult
     */
    @DeleteMapping("/{categoryId}")
    //   @PreAuthorize("@pms.hasPermission('wf:category:delete')")
    public AjaxResult deleteById(@PathVariable("categoryId") Long categoryId) {

        WorkflowCategory category = workflowCategoryService.getById(categoryId);

        setBaseEntity(category);

        category.setIsDeleted(EntityConstants.ENTITY_DEL_FLAT);

        boolean isSuccess = workflowCategoryService.updateById(category);

        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }


}
